//
//  CZComposeViewController.swift
//  GZWeibo007
//
//  Created by hdy on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD

enum CZComposeViewControllerType: Int {
    case Compose = 1
    case Repost = 2
    case Comment = 3
}

class CZComposeViewController: UIViewController {
    
    // MARK: - 属性
    /// 微博模型
    var status: CZStatus?
    
    /// 控制器类型
    private var controllerType: CZComposeViewControllerType
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init(controllerType: CZComposeViewControllerType) {
        self.controllerType = controllerType
        super.init(nibName: nil, bundle: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNavigationBar()
        setupTextView()
    }
    
    //设置导航栏
    private func setupNavigationBar() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "确定", style: UIBarButtonItemStyle.Plain, target: self, action: "sendStatus")
        
        setTitleView()
    }
    
    ///  设置导航栏标题
    private func setTitleView() {
        //设置控制器title
        var prefix = "评论微博"
        if controllerType == CZComposeViewControllerType.Repost{
            prefix = "转发微博"
            textView.placeholder = "转发微博"
        } else if controllerType == CZComposeViewControllerType.Comment {
            prefix = "评论微博"
            textView.placeholder = "评论微博"
        }
        
        // 获取用户名称
        if let name = CZUserAccount.loadUserAccount()?.screen_name {
            let titleName = prefix + "\n" + name
            
            let attrString = NSMutableAttributedString(string: titleName)
            
            let label = UILabel()
            
            label.numberOfLines = 0
            label.textAlignment = NSTextAlignment.Center
            label.font = UIFont.systemFontOfSize(14)
            
            // 截取用户名字
            let nameRange = (titleName as NSString).rangeOfString(name)
            
            // 设置属性文本的属性
            attrString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(12), range: nameRange)
            attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGrayColor(), range: nameRange)
            
            label.attributedText = attrString
            label.sizeToFit()
            
            navigationItem.titleView = label
        } else { /// 没有用户名
            navigationItem.title = prefix
        }
    }
    
    /// 发微博
    func sendStatus() {
        
        let text = textView.text
        
        if text == nil || text == "" {
            SVProgressHUD.showInfoWithStatus("内容不能为空")
            return
        }
        
       if controllerType == CZComposeViewControllerType.Repost { ///转发微博
            // 显示正在发送
            SVProgressHUD.showWithStatus("正在转发微博...", maskType: SVProgressHUDMaskType.Black)
        
            SVProgressHUD.showInfoWithStatus("转发成功")
            
            CZNetworkTool.sharedInstance.repostStatus(status!.id, status: text, finished: { (result, error) -> () in
                if error != nil {
                    SVProgressHUD.showErrorWithStatus("网络不给力...", maskType: SVProgressHUDMaskType.Black)
                    return
                }
                self.close()
            })
        } else if controllerType == CZComposeViewControllerType.Comment { /// 评论
            // 显示正在发送
            SVProgressHUD.showWithStatus("正在评论微博...", maskType: SVProgressHUDMaskType.Black)
            
            CZNetworkTool.sharedInstance.commentStatus(status!.id, comment: text, finished: { (result, error) -> () in
                if error != nil {
                    print("error:\(error)")
                    SVProgressHUD.showErrorWithStatus("网络不给力...", maskType: SVProgressHUDMaskType.Black)
                    return
                }
                self.close()
            })
        }
    }
    
    // 添加textView
    private func setupTextView(){
        view.addSubview(textView)
        textView.ff_Fill(view)
    }
    
    /// 关闭界面
    func close() {
        textView.resignFirstResponder()
        
        SVProgressHUD.dismiss()
        
        dismissViewControllerAnimated(true, completion: nil)
    }

    //MARK - 懒加载
    private lazy var textView: CZPlaceholderTextView = {
        let tv = CZPlaceholderTextView()
        
        tv.font = UIFont.systemFontOfSize(18)
        
        tv.backgroundColor = UIColor.brownColor()
        
        tv.alwaysBounceVertical = true
        
//        // 设置占位文本
//        tv.placeholder = "分享新鲜事"
        
        // 当拖动textView时,会让键盘退回去
        tv.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        
        return tv
    }()
}


